/**
 * Created by tanweiping on 2018/8/1.
 */
function logout() {
    sessionStorage.removeItem("user");
    $.ajax({
        url: application.contextPath + '/security/logout',
        success: function (d) {
            bootoast({
                message: d.message,
                type: d.isSuccess ? 'success' : 'warning',
                position: 'right-top',
                timeout: 2
            });
            setTimeout(function () {
                if (d.isSuccess) {
                    window.location.replace(application.contextPath + "/login/index");
                    return;
                }
            }, 2000);


        }
    })

}

//修改密码弹出
function showChange() {
    var html = "<div class=\"form-group form-row\" style='margin: 0 auto;width: 50%; margin-top: 20px;'>";
    html += "  <div class=\"input-group\" >";
    html += "<div class='input-group-addon'><font color='red'>原密码</font></div>";
    html += "<input type=\"text\" class=\"form-control\" id='oldPassWord' onblur='changePassWord()'/></div>";
    html += "</div>";
    html += "<div class=\"form-group form-row\" style='margin: 0 auto;width: 50%; margin-top: 20px;'>";
    html += "  <div class=\"input-group\" >";
    html += "<div class='input-group-addon'><font color='red'>新密码</font></div>";
    html += "<input type=\"password\" class=\"form-control\" id='newPassWord' readonly='readonly '/></div>";
    html += "</div>";
    html += "<div class=\"form-group form-row\" style='margin: 0 auto;width: 50%; margin-top: 20px;'>";
    html += "  <div class=\"input-group\" >";
    html += "<div class='input-group-addon'><font color='red'>二次确认</font></div>";
    html += "<input type=\"password\" class=\"form-control\" id='queryPassWord' readonly='readonly ' /></div>";
    html += "</div>";
    layer.open({
        title: '修改密码',
        type: 1,
        skin: 'layui-layer-rim', //加上边框
        area: ['500px', '300px'], //宽高
        content: html,
        btn: ['确定', '取消'],
        yes: function (index) {
            queryChange();
        }
    });
}

//验证密码输入密码是否正确
function changePassWord() {
    var oldPassWord = $("#oldPassWord").val();
    if (oldPassWord == '' || oldPassWord == null) {
        layer.msg('旧密码不得为空');
        return;
    }
    var url = application.contextPath + '/security/changePassWord';
    $.ajax({
        url: url,
        type: 'POST',
        async: false,
        dataType: 'json',
        data: {
            "oldPassWord": oldPassWord,
            "state": "1"
        },
        success: function (data) {
            if (data.state == '1') {
                layer.msg(data.message);
            } else {
                $("#newPassWord").removeAttr("readonly");
                $("#queryPassWord").removeAttr("readonly");
            }
        },
        error: function (data) {

        }
    })
}

//提交修改
function queryChange() {
    var oldPassWord = $("#oldPassWord").val();
    if (oldPassWord == '' || oldPassWord == null || oldPassWord == undefined) {
        layer.msg('旧密码不得为空');
        return;
    }
    var newPassWord = $("#newPassWord").val();
    if (newPassWord == '' || newPassWord == null || newPassWord == undefined) {
        layer.msg('新密码不得为空');
        return;
    }
    if(oldPassWord ==newPassWord){
        layer.msg('新密码不得和旧密码一致');
        return;
    }
    var queryPassWord = $("#queryPassWord").val();
    if (queryPassWord != newPassWord) {
        layer.msg('两次密码输入不一致');
        return;
    }
    var url = application.contextPath + '/security/changePassWord';
    $.ajax({
        url: url,
        type: 'POST',
        async: false,
        dataType: 'json',
        data: {
            "oldPassWord": oldPassWord,
            "newPassWord": newPassWord,
            "state": "2"
        },
        success: function (data) {
            if (data.state == '1') {
                layer.msg(data.message);
            } else {
                layer.alert('修改成功');
                logout();
            }
        },
        error: function (data) {

        }
    })
}